package com.ruoyi.common.base.biz.controller.v3;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.base.biz.service.biz.BaseBizService;
import com.ruoyi.common.core.domain.BizBaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.ExcelImportBaseTemplate;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;

/**
 * BaseController
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2024/1/25 16:23
 */
public class BaseController<E extends BizBaseEntity, Req extends E, Res extends E,
        BizS extends BaseBizService<E, Req, Res>>
        extends com.ruoyi.common.base.biz.controller.v2.BaseController<E, Req, Res, BizS> {

    private ExcelImportBaseTemplate excelImportBaseTemplate;

    @Override
    public TableDataInfo paging(@RequestBody Req req) {
        PageInfo<Res> pageInfoRes = this.getBizService().paging(req);
        return getDataTable(pageInfoRes);
    }

    /**
     * 模板下载
     * @param response
     */
    @PostMapping("/download")
    public void download(HttpServletResponse response) {
        try {
            ExcelUtils.downloadExcelTemplateRuoYi(response, getExcelImportBaseTemplate().getClass(), getExcelImportBaseTemplate());
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("系统异常，模板生成失败，请稍后重试");
        }
    }

    public ExcelImportBaseTemplate getExcelImportBaseTemplate() {
        return excelImportBaseTemplate;
    }
}
